import usersActions from 'actions/users'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {},
  reducers: {
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 登陆明细
    async getLoginList (params) {
      this.save({
        loginData: null
      })
      const res = await usersActions.getLoginList(params)
      if (res.success) {
        this.save({
          loginData: {
            list: res.data && res.data.loginRecordList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 用户列表
    async getUserList (params) {
      this.save({
        userData: null
      })
      const res = await usersActions.getUserList(params)
      if (res.success) {
        this.save({
          userData: {
            userList: res.data && res.data.userResList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 获取用户详情
    async getUserDetails (params) {
      this.save({
        userDetail: null
      })
      const res = await usersActions.getUserDetails(params)
      if (res.success) {
        this.save({
          userDetail: res.data
        })
      }
    },
    // 收藏明细
    async getCollectionDetails (params) {
      this.save({
        collectionData: null
      })
      const res = await usersActions.getCollectionDetails(params)
      if (res.success) {
        this.save({
          collectionData: res.data
        })
      }
    },
    // 关注明细
    async getFollowList (params) {
      this.save({
        followData: null
      })
      const res = await usersActions.getFollowList(params)
      if (res.success) {
        this.save({
          followData: res.data
        })
      }
    },
    // 购物车明细
    async getShoppingCarList (params) {
      this.save({
        shoppingCarData: null
      })
      const res = await usersActions.getShoppingCarList(params)
      if (res.success) {
        this.save({
          shoppingCarData: res.data
        })
      }
    },
    // 点赞明细
    async getlikeList (params) {
      this.save({
        likeData: null
      })
      const res = await usersActions.getlikeList(params)
      if (res.success) {
        this.save({
          likeData: res.data
        })
      }
    },
    // 获取规则列表
    async getRulesList (params) {
      this.save({
        ruleData: null
      })
      const res = await usersActions.getRulesList(params)
      if (res.success) {
        this.save({
          ruleData: {
            list: res.data && res.data.pcUserTypeList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 删除规则
    async delRules (params) {},
    // 编辑规则
    async editRules (params) {
      this.save({
        editRequest: false
      })
      const res = await usersActions.editRules(params)
      if (res.success) {
        this.save({
          editRequest: true
        })
      }
    },
    // 设置默认
    async setRules (params) {
      const res = await usersActions.setRules(params)
      if (res.success) {
        message.success('设置成功')
      }
    },
    // 获取邀请列表
    async getInviteList (params) {
      this.save({
        inviteData: null
      })
      const res = await usersActions.getInviteList(params)
      if (res.success) {
        this.save({
          inviteData: {
            list: res.data && res.data.invitedList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 获取邀请详情
    async getInviteDetails (params) {
      this.save({
        inviteDetailsData: null
      })
      const res = await usersActions.getInviteDetails(params)
      if (res.success) {
        this.save({
          inviteDetailsData: res.data
        })
      }
    },
    // 获取用户收货列表
    async getAddressList (params) {
      this.save({
        addressList: null
      })
      const res = await usersActions.getAddressList(params)
      if (res.success) {
        this.save({
          addressList: res.data
        })
      }
    },
    // 获取用户心淡信息
    async getOrderList (params) {
      this.save({
        orderData: null
      })
      const res = await usersActions.getOrderList(params)
      if (res.success) {
        this.save({
          orderData: {
            total: res.data.totalCount,
            list: res.data.orderList
          }
        })
      }
    },
    // 积分列表
    async getScoreList (params) {
      this.save({
        scoreData: null
      })
      const res = await usersActions.getScoreList(params)
      if (res.success) {
        this.save({
          scoreData: {
            total: res.data.total,
            list: res.data.userIntegrationListNode
          }
        })
      }
    },
    // // 存储积分列表当条数据
    // setInteralDetail (params) {
    //   this.save({
    //     interalDetail: params
    //   })
    // },
    // 积分明细
    async getIntegralDetails (params) {
      this.save({
        integralDetailsData: {}
      })
      const res = await usersActions.getScoreDetailList(params)
      if (res.success) {
        this.save({
          integralDetailsData: res.data
        })
      }
    },
    // 修改用户积分
    async updateScore (params) {
      await usersActions.updateScore(params)
    },
    // 积分规则
    async integrationRule (params) {
      this.save({
        rules: []
      })
      const res = await usersActions.integrationRule(params)
      this.save({
        rules: res.data
      })
    },
    // 保存标签
    async saveLabel (params) {
      this.save({
        saveLabelRequest: false
      })
      const res = await usersActions.saveLabel(params)
      if (res.success) {
        this.save({
          saveLabelRequest: true
        })
      } else {
        message.error(res.msg)
      }
    }
  })
}
